package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;

import com.gxuwz.medical.domain.policy.Policy;

/**
 * 慢性病政策管理请求处理模块
 * @author 沉羽墨玖
 *
 */
public class PolicyServlet extends BaseServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m=req.getParameter("m");
	    if("list".equals(m)){
	      process(req, resp, "/page/policy/policy_list.jsp");
	    }else if("input".equals(m)){
		  process(req, resp, "/page/policy/policy_add.jsp");
		}else if("get".equals(m)){
			  process(req, resp, "/page/policy/policy_edit.jsp");
		}else if("add".equals(m)){
			add(req, resp);
		}else if("edit".equals(m)){
			edit(req, resp);
		}else if("del".equals(m)){
			del(req, resp);
		}else{
			error(req,resp);
		}
	
	}
	/**
	 * 添加慢性病政策信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

  	  //1:接收参数
  	  String runYear=req.getParameter("runYear");
  	  String maxlinestr=req.getParameter("maxline");
  	  String status=req.getParameter("status");
  	  String remark=req.getParameter("remark");
  	  double ratio=Double.parseDouble(req.getParameter("ratio"));
  	  double maxline=0.00;
  	if(NumberUtils.isNumber(maxlinestr)){
 		 maxline =Double.parseDouble(maxlinestr);
 	  }
  	  //2:构造新慢性病政策信息对象
  	  Policy policy=new Policy(runYear, maxline, status, remark,ratio);
  	  //3:调用保存的方法
  	  try{
  		policy.add();
  		process(req, resp, "/page/policy/policy_list.jsp");
  	  }catch(Exception e){
  		e.printStackTrace();
  	  }
	
  
	}
	/**
	 * 修改慢性病政策信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void edit(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

	  	  //1：接收参数
	  	  String runYear=req.getParameter("runYear");
	  	  double maxline=Double.parseDouble(req.getParameter("maxline"));
	  	  String status=req.getParameter("status");
	  	  String remark=req.getParameter("remark");
	  	  double ratio=Double.parseDouble(req.getParameter("ratio"));
	  	  //2:构造新慢性病政策信息对象
	  	  Policy policy=new Policy(runYear, maxline, status, remark,ratio);
	  	  //3：调用保存的方法
	  	  try{
	  		policy.edit();
	  		process(req, resp, "/page/policy/policy_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
		
	  
		}
	private String setup(HttpServletRequest req,HttpServletResponse resp,String status)throws Exception{

	  	  //1：接收参数
	  	  String runyear=req.getParameter("id");
	  	  //2:构造对象
	      Policy model=new Policy(runyear);
	      model.setup(status);
	  	  return "/page/policy/policy_list.jsp";
	  	  
		}
	/**
	 * 删除慢性病政策信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void del(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

	  	  //1：接收参数
	  	  String runYear=req.getParameter("runYear");
	  	  
	  	  //2:构造新慢病信息对象
	  	  Policy policy=new Policy();
	  	  try{
	  		System.out.println(runYear);
	  		policy.del(runYear);
	  		process(req, resp, "/page/policy/policy_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
	  
		}
}
